package com.astro.dao;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.astro.domain.bean.User;

@Repository("userDAO")
public class UserDAO extends BaseHelperDAO {

	public static final String USER_NAME	= "customerName";
	public static final String LOGIN_ID 	= "loginId";

	public List<User> findByUserName(Object customerName) throws Exception {
		return findByProperty(USER_NAME, customerName);
	}
	
	public List<User> findByLoginId(Object loginId) throws Exception {
		return findByProperty(LOGIN_ID, loginId);
	}
	
	@SuppressWarnings("unchecked")
	public List<User> findByProperty(String propertyName, Object value) throws Exception {

		try {

			String queryString = "from User as model where model."
					+ propertyName + "= :propertyName";
			Query query = getCurrentSession().createQuery(queryString);
			query.setParameter("propertyName", value);
			return query.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<User> listUser() throws Exception {
		List<User> result = null;
		
		String queryString = "from User"; // Hibernate Query Language
		result = getCurrentSession().createQuery(queryString).list();
		
		return result;
	}
	
	/**
	 * Insert customer into database
	 */
	public void addUser(User entity) throws Exception {
		// Insert into database
		getCurrentSession().save(entity);
	}

}